package com.production.CoursePlatform.controller;

import com.production.CoursePlatform.dto.NoticeWithIsReadDTO;
import com.production.CoursePlatform.entity.Notice;
import com.production.CoursePlatform.response.ApiResponse;
import com.production.CoursePlatform.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    // 此接口已检验
    @Autowired
    private NoticeService noticeService;

    // 创建公告
    @PostMapping("/create")
    public ApiResponse createNotice(@RequestBody Notice notice) {
        return noticeService.addNotice(notice);
    }

    // 根据 cn_id 删除公告
    @DeleteMapping("/delete/{cnID}")
    public ApiResponse deleteNotice(@PathVariable("cnID") int cnID) {
        return noticeService.deleteNoticeById(cnID);
    }

    // 更新公告
    @PutMapping("/update")
    public ApiResponse updateNotice(@RequestBody Notice notice) {
        return noticeService.updateNotice(notice);
    }

    // 根据 cn_id 查找公告
    @GetMapping("/get/{cnID}")
    public Notice getNoticeById(@PathVariable("cnID") int cnID) {
        return noticeService.findNoticeById(cnID);
    }

    // 根据 ct_id 查找公告
    @GetMapping("/getByCourse/{ctID}")
    public List<Notice> getNoticesByCourseId(@PathVariable("ctID") int ctID) {
        return noticeService.findNoticesByCtId(ctID);
    }

    // 根据 s_id 查找所有公告
    @GetMapping("/getAll/{sID}")
    public ResponseEntity<List<NoticeWithIsReadDTO>> findAllNotices(@PathVariable("sID") int sID) {
        List<NoticeWithIsReadDTO> notices = noticeService.findAllNoticesBySID(sID);
        return ResponseEntity.ok(notices);
    }

}
